package com.weixingtu.commuterCar.tool.gps.impl;

import com.alibaba.fastjson.JSONObject;
import com.weixingtu.commuterCar.tool.gps.GPS;
import com.weixingtu.commuterCar.tool.gps.GPSInfo;
import com.weixingtu.commuterCar.tool.utils.HttpSend;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

@Deprecated
public class CZJGps extends GPSInfo implements GPS {

    public CZJGps(){

    }
    @Override
    public void init() {
        super.setServiceUrl("http://www.aika168.com/api");
        List<String> list = new ArrayList<>();
        list.add("9172006138");
        super.setImei(list);
        super.setKey("XINGMAI20180517UDY27S");
    }

    @Override
    public List<JSONObject> getPosition() {
        if(getKey().isEmpty()){
            System.out.println("getPosition-key:null");
            return null;
        }
        JSONObject jsonParams = preparePositionParam();
        String result = HttpSend.sendGet(getServiceUrl()+"/Tracking.aspx",jsonParams);
        return handlePositionResult(result);
    }

    private JSONObject preparePositionParam(){
        JSONObject jsonParams = new JSONObject(new LinkedHashMap<>());
        String imeiString = "";
        if(getImei().size()==1){
            imeiString = getImei().get(0);
        }else{
            imeiString = getImei().get(0)+",";
            for(int i=0;i<getImei().size();i++){
                imeiString+= "," + getImei().get(i);
            }
        }
        jsonParams.put("sn",imeiString);
        jsonParams.put("mapType","Baidu");
        jsonParams.put("key",getKey());
        System.out.println("params:"+jsonParams);
        return jsonParams;
    }

    private List<JSONObject> handlePositionResult(String result){
        if(result.isEmpty()){
            System.out.println("getPosition-ret:null");
            return null;
        }
        JSONObject jsonRet = getMapRet(result);
        System.out.println(jsonRet);
        if(jsonRet.containsKey("state")&&"0".equals(jsonRet.get("state"))){
            List<JSONObject> realList = new LinkedList<>();
            JSONObject jsonResult = new JSONObject(new LinkedHashMap<>());
            jsonResult.put("imei",getImei().get(0));
            jsonResult.put("status", jsonRet.get("isStop"));
            jsonResult.put("lng",jsonRet.get("lng"));
            jsonResult.put("lat",jsonRet.get("lat"));
            jsonResult.put("speed", jsonRet.get("speed"));
            jsonResult.put("course", jsonRet.get("course"));
            jsonResult.put("positionType", jsonRet.get("isGPS"));
            System.out.println(jsonResult);

            realList.add(jsonResult);
            return realList;
        }
        return null;
    }
}
